//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<frame_control.h>
///	@path	~/src/engine/xcore/
///	@date	2007/12/02
///	@desc	Frame control of engine.

#pragma once

#include "xeres.h"

#include "graphics/animation/frame.h"

#include "engine/xcore/time_control.h"

namespace xeres {

	/*!
		\class	FrameControl
		\brief	Frame control of engine.
	*/
	class FrameControl : public TimeControl
	{
	protected:

		/// \name Constructor & Destructor
		//@{

		/// \ctor
		FrameControl( void );

		/// \dtor
		~FrameControl( void );

		//@}

		/// \name Internal Manipulation
		//@{

		/*!
			\brief		Do first frame initialization, pass super frame count.
		*/
		void FirstFrame( uint superFrame );

		/*!
			\brief		Calculate time gap & controls frame rate (will call sleep to delay update).
		*/
		bool UpdateFrame( void );

		//@}

	public:

		/// \name Interface Manipulation
		//@{

		/*!
			\brief		Set a frame limit to limit update count.
		*/
		void SetFrameLimit( float fps );

		/*!
			\brief		Set frame rate balance.
		*/
		void BalanceFrameRate( bool enable , const Time& period );

		/*! 
			\brief		Get super frame state.
		*/
		bool IsSuperFrame( void ) const { return superframe() > 0; }

		//@}

	private:

	//@ data

		Counter				m_updateCounter;
		Time				m_updateTime;

		// controlling update
		float				m_renderUpdate;

		float				m_fpsLimit; // fps limit
		float				m_gapLimit; // 1/fps limit
		Time				m_timeLimit; // 1/fps to time

		uint				m_balanceCounter;
		Time				m_balanceTime;
		Time				m_lastBalance;
		float				m_balanceFps; // frame balance
	};

} // namespace xeres
